package cn.xiaoliu.org.service.impl;

import cn.xiaoliu.basic.exception.BusinessException;
import cn.xiaoliu.basic.service.impl.BaseServiceImpl;
import cn.xiaoliu.org.domain.Employee;
import cn.xiaoliu.org.domain.Shop;
import cn.xiaoliu.org.mapper.EmployeeMapper;
import cn.xiaoliu.org.mapper.ShopMapper;
import cn.xiaoliu.org.service.IEmployeeService;
import cn.xiaoliu.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IEmployeeService employeeService;

    @Override
    public void settlement(Shop shop) {
        //一.判断
        //1.参数不能为空
        //2.判断密码和确认密码是否一致
        Employee admin = shop.getAdmin();
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("您输入的两次密码不一致");
        }
        //3.判断账号是否已经注册过在employee表中对比username，phone和email，如果已经注册过提示直接登录
        Employee employee = employeeMapper.loadByUser(admin.getUsername());
        if (employee!=null){
            throw new BusinessException("此用户已经存在，请直接登录，如果忘记密码，请走找回流程");
        }

        //二.保存数据
        //1.先保存员工信息，返回自增id
        //employeeMapper.save(admin);在保存employee的时候还需要级联保存Logininfo
        employeeService.add(admin);//员工相关的业务应该给员工去完成
        //2.设置shop中的admin的id，然后保存shop，返回shop自增id
        shop.setAdmin(admin);
        shopMapper.save(shop);
        //3.设置employee中的shop_id，再修改employee
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }
}
